"use strict";

/**
 * Post code to Server
 * @param {string} url
 * @param {string} code code
 * @param {function} handler How to deal with response from Server
 */
function postCode(url, code, handler) {
    let xmr = new XMLHttpRequest();
    xmr.open("POST", url, true);
    xmr.onreadystatechange = () => {
        if (xmr.readyState == 4 && xmr.status == 200) {
            if (handler instanceof Function) {
                handler(xmr.responseText);
            }
        }
    };
    xmr.send(code);
}

/**
 * Get analysis result from Server
 * @param {string} url 
 * @param {string} serialCode 
 * @param {functoin} handler How to deal with response from Server
 */
function getAnalysis(url, serialCode, handler) {
    let xmr = new XMLHttpRequest();

    if (!url.endsWith("/")) url += "/";

    xmr.open("GET", url + serialCode, true);
    xmr.onreadystatechange = () => {
        if (xmr.readyState == 4 && xmr.status == 200) {
            if (handler instanceof Function) {
                handler(xmr.responseText);
            }
        }
    };
    xmr.send();
}